package com.chinacoal.microservice.impl.util;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import sun.net.ftp.FtpClient;


import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.List;

public class ErpInvokeInterface {

    /**
     * 调用ERP系统接口
     * @param taskId
     * @param url
     * @return
     */
    public String getTaskDetail(String taskId, String url) {
        String result = "";
        CloseableHttpResponse response = null;
        List<BasicNameValuePair> nameValuePairs = new LinkedList<>();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            nameValuePairs.add(new BasicNameValuePair("seqId", taskId));
            String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs));
            HttpGet httpGet = new HttpGet(appendString(url, paramStr));
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 将GET请求参数通过?拼接
     * @param url
     * @param paramStr
     * @return
     */
    private String appendString(String url, String paramStr) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(url);
        stringBuffer.append("?");
        stringBuffer.append(paramStr);
        return stringBuffer.toString();
    }


    // FTP客户端
    public FtpClient ftpClient;

    /**
     * 服务器连接
     *
     * @param ip
     *            服务器IP
     * @param port
     *            服务器端口
     * @param user
     *            用户名
     * @param password
     *            密码
     * @param path
     *            服务器路径
     */
    public void connectServer(String ip, int port, String user, String password, String path)
    {
        try {
            /****** 连接服务器的两种方法****** */
            // 第一种方法
            // ftpClient = new FtpClient();
            // ftpClient.openServer(ip, port);
            // 第二种方法
            InetSocketAddress addr = new InetSocketAddress(ip,port);
            ftpClient = FtpClient.create(addr);
            ftpClient.login(user, password.toCharArray());
            // 设置成2进制传输
            ftpClient.setBinaryType();
            if (path.length() != 0) {
                // 把远程系统上的目录切换到参数path所指定的目录
                ftpClient.changeDirectory(path);
            }
            ftpClient.setBinaryType();

        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnect()
    {
        try {
            //ftpClient.closeServer();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

}
